package com.example.demo.controller;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.util.ByteArrayDataSource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Properties;

@Controller
@RequestMapping("/index")
public class controller {

//    @Scheduled(cron = "* * * * * 1-7")
    @ResponseBody
    private void Remind(){
        String smtphost = "smtp.163.com"; // 发送邮件服务器
        String userid = "zzh5201314tun@163.com"; // 邮件服务器登录用户名
        String password = "HCMKIQOPXUUQMVAV"; // 邮件服务器登录密码
        String from = "zzh5201314tun@163.com"; // 发送人邮件地址
        String to="1475700051@qq.com";
        String subject = "豚豚提醒"; // 邮件标题
        String result = "";
        try {
            //初始化Properties类对象
            Properties props = new Properties();
            //设置mail.smtp.host属性
            props.put("mail.smtp.host", smtphost);
            //设置使用验证
            props.put("mail.smtp.auth", "true");
            // 获取非共享的session对象
            Session ssn = Session.getInstance(props, null);
            //创建一个默认的MimeMessage对象。
            MimeMessage message = new MimeMessage(ssn);
            //设置From: 头部的header字段
            message.setFrom(new InternetAddress(from));
            //设置 To: 头部的header字段
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            //设置 Subject: header字段
            message.setSubject(subject);

            // 现在设置的实际消息
            message.setText("小笨蛋，憨憨叫renjia");

            //定义发送协议
            Transport transport = ssn.getTransport("smtp");
            //建立与服务器的链接
            transport.connect(smtphost, userid, password);
            //发送邮件
            transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
            //transport.send(message);
            //关闭邮件传输
            transport.close();
            //捕获异常
            result = "定时发送成功";
        } catch (Exception m) {
            result = "定时发送失败";
            m.printStackTrace();
        }
        System.out.println("结果："+result);
    }

    @RequestMapping("/sendxls")
    @ResponseBody
    public static boolean sendEmail(String to, ByteArrayOutputStream baos) {
        String smtphost = "smtp.163.com"; // 发送邮件服务器
        String userid = "zzh5201314tun@163.com"; // 邮件服务器登录用户名
        String password = "HCMKIQOPXUUQMVAV"; // 邮件服务器登录密码
        String from = "zzh5201314tun@163.com"; // 发送人邮件地址
        String subject = "豚豚记账数据"; // 邮件标题

        try{
            //初始化Properties类对象
            Properties props = new Properties();
            //设置mail.smtp.host属性
            props.put("mail.smtp.host", smtphost);
            //设置使用验证
            props.put("mail.smtp.auth", "true");
            // 获取非共享的session对象
            Session ssn = Session.getInstance(props, null);
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(ssn);
            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));
            // Set To: 头部头字段
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
            // Set Subject: 头部头字段
            message.setSubject(subject);
            /*添加附件*/
            Multipart multipart = new MimeMultipart();
            if(baos != null) {
                MimeBodyPart fileBody = new MimeBodyPart();
                DataSource source = new ByteArrayDataSource(baos.toByteArray(), "application/msexcel");
                fileBody.setDataHandler(new DataHandler(source));
                // 中文乱码问题
                fileBody.setFileName(MimeUtility.encodeText("记账表.xlsx"));
                multipart.addBodyPart(fileBody);
            }
            message.setContent(multipart);

            //定义发送协议
            Transport transport = ssn.getTransport("smtp");
            //建立与服务器的链接
            transport.connect(smtphost, userid, password);
            //发送邮件
            transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
            //关闭邮件传输
            transport.close();
        }catch (MessagingException mex) {
            mex.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }


}
